package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqupt.qualityevaluation.dao.EvaluateSubmitFilesMapper;
import com.cqupt.qualityevaluation.dao.EvaluateSubmitMapper;
import com.cqupt.qualityevaluation.pojo.evaluatesubmit;
import com.cqupt.qualityevaluation.pojo.evaluatesubmitfiles;
import com.cqupt.qualityevaluation.pojo.school;
import com.cqupt.qualityevaluation.pojo.schoolhostcode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/submit")
public class EvaluateSubmitController {

    @Autowired
    EvaluateSubmitMapper evaluateSubmitMapper;
    @Autowired
    EvaluateSubmitFilesMapper evaluateSubmitFilesMapper;

    @GetMapping("/search/{schoolname}/{taskid}/{index3id}")
    public evaluatesubmit search(@PathVariable("schoolname") String schoolname
            ,@PathVariable("taskid") int taskid ,@PathVariable("index3id") int index3id){
        QueryWrapper<evaluatesubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname", schoolname)
                .eq("evaluatetaskid", taskid)
                .eq("evaluateindex3id", index3id);
        evaluatesubmit evaluatesub=evaluateSubmitMapper.selectOne(queryWrapper);

        return evaluatesub;
    }

    @GetMapping("/searchfiles/{submitid}")
    public List<evaluatesubmitfiles> searchFiles(@PathVariable("submitid") int submitid){
        QueryWrapper<evaluatesubmitfiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("submitid",submitid);
        return evaluateSubmitFilesMapper.selectList(queryWrapper);
    }
    @PostMapping("/update")
    public Object update(@RequestBody evaluatesubmit es){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<evaluatesubmit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", es.getId());
        evaluatesubmit evaluatesubmit=new evaluatesubmit();
        evaluatesubmit.setEvaluatetaskid(es.getEvaluatetaskid());
        evaluatesubmit.setEvaluateindex3id(es.getEvaluateindex3id());
        evaluatesubmit.setSubmitcontent(es.getSubmitcontent());
        try{
            result=evaluateSubmitMapper.update(evaluatesubmit,updateWrapper);
        }catch (Exception e){
            map.put("code","fail");
            map.put("msg","该学校名称已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
//    @PutMapping("/update")
//    public Object update(@RequestBody evaluatesubmit s){
//        System.out.println(s);
//        Map map=new HashMap<>();
//        int result=0;
//        UpdateWrapper<evaluatesubmit> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("schoolname",s.getSchoolname()).eq("evaluatetaskid",s.getEvaluatetaskid()).eq("evaluateindex3id",s.getEvaluateindex3id());
//        System.out.println(updateWrapper);
//        evaluatesubmit evaluatesubmit=new evaluatesubmit();
//        evaluatesubmit.setEvaluatetaskid(s.getEvaluatetaskid());
//        evaluatesubmit.setEvaluateindex3id(s.getEvaluateindex3id());
//        evaluatesubmit.setSubmitcontent(s.getSubmitcontent());
//        try{
//            result=evaluateSubmitMapper.update(evaluatesubmit,updateWrapper);
//        }catch (Exception e){
//            map.put("code","fail");
//            map.put("msg","该学校名称已存在");
//            return map;
//        }
//        if (result==1){
//            map.put("code","success");
//            map.put("msg","更新成功");
//        }
//        else {
//            map.put("code", "fail");
//            map.put("msg","添加失败");
//        }
//        return map;
//    }
}
